package com.ffspace.crawlers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import cn.wanghaomiao.seimi.annotation.Crawler;
import cn.wanghaomiao.seimi.def.BaseSeimiCrawler;
import cn.wanghaomiao.seimi.struct.Request;
import cn.wanghaomiao.seimi.struct.Response;
import cn.wanghaomiao.xpath.model.JXDocument;

import com.ff.blog.enums.BlogSource;
import com.ff.blog.util.BeanUtil;
import com.ffspace.blog.core.dal.dataobject.Blog;
import com.ffspace.blog.service.BlogService;


@Crawler(name = "basic")
public class Basic extends BaseSeimiCrawler {
		
	/*//@Autowired
*/	
/*	@Resource(name="blogService")
	private BlogService blogService;*/
	
	BlogService blogService = (BlogService)BeanUtil.getBean("blogService");
	
    @Override
    public String[] startUrls() {
        return new String[]{"http://www.cnblogs.com/"};
    }
    @Override
    public void start(Response response) {
        JXDocument doc = response.document();
        try {
            List<Object> urls = doc.sel("//a[@class='titlelnk']/@href");
            logger.info("{}", urls.size());

            for (Object s:urls){
                push(new Request(s.toString(),"getTitle"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void getTitle(Response response){
        JXDocument doc = response.document();
        try {
        	
        	String linkUrl = response.getUrl();
        	String title = doc.sel("//h1[@class='postTitle']/a/text()|//a[@id='cb_post_title_url']/text()").toString();
            logger.info("url:{} {}", linkUrl, title);           
            String content = response.getContent();//doc.sel("//div[@id='cnblogs_post_body']/allText()").toString();

            logger.info("content:{}", content);
            Blog blog = new Blog();
            blog.setTitle(title);
            blog.setContent(content);
            blog.setLinkUrl(linkUrl);
            blog.setSource(BlogSource.CNBLOG.getCode());
            blog.setUserId(0);
            blogService.saveOrUpdateBlogByLinkUrl(blog);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
  @Scheduled(cron = "0/5 * * * * ?")
  public void callByCron(){
      logger.info("我是一个根据cron表达式执行的调度器，5秒一次");
      // 可定时发送一个Request
      push(Request.build(startUrls()[0],"start").setSkipDuplicateFilter(true));
  }
  
  
}